﻿package {
        import flash.display.Sprite;
        import flash.display.Shape;
        import flash.events.Event;
     


        public class HitBall extends Sprite {

                private var b:Ball;
                private var r:Number;
                private var speedX;
                private var speedY;
                private var fraction;//地面摩擦力
                private var chi;
                private var d;


                public function HitBall():void {


                        b=new Ball(0,0,0x000000,20);
                        b.x=275;
                        b.y=200;
                        r=20;
                        d=100;
                        speedX=0;
                        speedY=0;
                        chi=0;// 斥力初始為0，使球一開始為靜止
                        fraction=0.95;//摩擦力

                        addChild(b);

                        addEventListener(Event.ENTER_FRAME,Hit);



                }
                private function Hit(e:Event):void {

                                var dis=Math.sqrt((b.x-mouseX)*(b.x-mouseX)+(b.y-mouseY)*(b.y-mouseY));//滑鼠和球的距離(開根號)


                        //球碰壁
                        if (b.x<=r) {
                                speedX*=-1;
                                b.x=20;
                        }
                        if (b.x>=stage.stageWidth-r) {

                                speedX*=-1;
                                b.x=stage.stageWidth-r;
                        }
                        if (b.y<=20) {

                                speedY*=-1;
                                b.y=20;
                        }
                        if (b.y>=stage.stageHeight-r) {

                                speedY*=-1;
                                b.y=stage.stageHeight-r;
                        }
                        if (dis<=d) {

                                chi=8-dis*0.01;//斥力，隨著距離增加而減少
                                speedX+=chi*(b.x-mouseX)/dis;//X軸加速度
                                speedY+=chi*(b.y-mouseY)/dis;//Y軸加速度


                                b.x+=speedX;
                                b.y+=speedY;


                        }
                        speedX*=fraction;//摩擦力
                        speedY*=fraction;

                        b.x+=speedX;
                        b.y+=speedY;


                }
        }
}